package com.atguigu.lease.web.admin.custom.config;

import com.atguigu.lease.web.admin.custom.converter.StringToBaseEnumConverterFactory;
import com.atguigu.lease.web.admin.custom.converter.StringToItemTypeConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.FormatterRegistry;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Component
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private StringToItemTypeConverter stringToItemTypeConverter;

/*  2.(风)响应时HttpMessage处理,而不是Converter处理,枚举到String转化只能用@JsonValue
ChatGPT:
Spring MVC 的 Converter只参与Web数据绑定（如参数、路径变量、表单），不会参与 JSON 响应序列化
JSON 响应由 HttpMessageConverter（尤其是 Jackson）处理，与 FormatterRegistry 无关
    @Autowired
    private ItemTypeToStringConverter itemTypeToStringConverter;
*/

    @Autowired
    private StringToBaseEnumConverterFactory stringToBaseEnumConverterFactory;

/*  1.注册到类型转化器FormatterRegistry,而不是消息转换器HttpMessageConverter
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add((HttpMessageConverter<?>) stringToItemTypeConverter);
    }
*/

    @Override
    public void addFormatters(FormatterRegistry registry) {
        //1.将ItemType转化器注册到FormatterRegistry
        registry.addConverter(stringToItemTypeConverter);

        //2.(风)响应时HttpMessage处理,而不是Converter处理,枚举到String转化只能用@JsonValue
        //registry.addConverter(itemTypeToStringConverter);

        //3.将转换工厂注册到FormatterRegistry
        registry.addConverterFactory(stringToBaseEnumConverterFactory);
    }




}
